package org.mao.util;

public class StringUtil {
	
	private static final Integer HEX_STRING_PERIOD = 4;

	public static String toHex(String sourceStr) {
		StringBuilder sb = new StringBuilder(sourceStr.length() * HEX_STRING_PERIOD);
		for (int i = 0; i < sourceStr.length(); i++) {
			String strHex = Integer.toHexString(sourceStr.charAt(i));
			strHex = "0000000".substring(0, HEX_STRING_PERIOD - strHex.length()) + strHex;
			strHex = StringUtil.reverse(strHex);
			sb.append(strHex);
		}
		return sb.toString();
	}

	public static String reverse(String strHex) {
		char[] chars = strHex.toCharArray();
		char[] resultChar = new char[strHex.length()];
		for (int i = 0; i < strHex.length(); i++) {
			resultChar[strHex.length() - i - 1] = chars[i];
		}
		return new String(resultChar);
	}

}
